package de.uni_stuttgart.ipvs.ids.nameserver;

/**
 * Main class for testing the Nameserver task.
 * @author Team 3/4 - Elkhan Dadashov, Dhaval A Shah, Patrick Stortz
 */
public class Main {
	
	/**
	 * Main method for testing the Nameserver task.
	 * @param args No arguments are required/used.
	 */
	public static void main(String[] args) {
		
		// instantiate the name servers
		Nameserver root = new Nameserver("root", null, 4000, 4001);
		Nameserver de = new Nameserver("de", root, 4002, 4003);
		Nameserver com = new Nameserver("com", root, 4004, 4005);
		Nameserver uniStuttgart = new Nameserver("uni-stuttgart", de, 4006, 4007);
		Nameserver google = new Nameserver("google", de, 4008, 4009);
		Nameserver informatik = new Nameserver("informatik", uniStuttgart, 4010, 4011);

		// create the hierarchy
		root.addChild("de", de);
		root.addChild("com", com);
		de.addChild("uni-stuttgart", uniStuttgart);
		de.addChild("google", google);
		uniStuttgart.addChild("informatik", informatik);
		informatik.addNameAddressPair("ipvs", "192.168.0.1");
		google.addNameAddressPair("foo", "192.169.0.2");

		// wait some milliseconds until all threads are running
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// we got interrupted, just continue
		}

		// create client and request
		Client client = new Client();
		String name1 = "ipvs.informatik.uni-stuttgart.de";
		String name2 = "foo.google.de";
		String address1, address2;
		
		// address resolution for part a)
		address1 = client.startRecursiveNameResolution(name1, "localhost", 4000);
		address2 = client.startRecursiveNameResolution(name2, "localhost", 4000);
		System.out.println("Recursive: Address for " + name1 + " is: " + address1);
		System.out.println("Recursive: Address for " + name2 + " is: " + address2);
		
		// address resolution for part b)
		address1 = client.startIterativeNameResolution(name1, "localhost", 4001);
		address2 = client.startIterativeNameResolution(name2, "localhost", 4001);
		System.out.println("Iterative: Address for " + name1 + " is: " + address1);
		System.out.println("Iterative: Address for " + name2 + " is: " + address2);
		
		// end the application to shut down the listeners
		System.exit(0);
	}
}
